var Oasis = artifacts.require("./OsGameLogic.sol");
var GameConfig = artifacts.require("./OsGameConfig.sol");

/**
 * @api createPlayer (sendTransaction)
 * @param address adr 玩家的账户
 * @param string name 名字
 * @param uint16[4] properties 四维属性
 * @event PlayerCreate(address indexed _user, uint256 _time)
 * @event ItemCreated(address user, uint index, uint tokenId)
 * 
 * @api getPlayerInfo(call)
 * @param address adr
 * @returns {[bool, string, uint256[10], uint16, uint16,uint16, uint16]} 
 * 对应玩家的valid, name, store, strength, constitution, intelligence, lucky
 * 
 * @api getItemInfo(call)
 * @param uint256 tokenId
 * @returns {[bool, bool, uint256, uint256, uint16, address, uint16, uint16, uint16, uint16]}
 * 对应物品的valid, locked, tokenId, itemId, useCount, owner, strength, constitution, intelligence, lucky
 * 
 * @api createOneTrans(sendTransaction) 创建一个交易
 * @param address adr
 * @param uint256 tokenId 物品的标识id
 * @param uint32 value 物品的价格(单位wei)
 * 
 * @api getTransactionInfo(call) 
 * @param uint256 transId 交易id
 * @returns {[bool, address, uint256, uint256, uint32, uint32]}
 * 对应交易的valid, seller, tokenId, transId, createTime, weiValue
 * 
 * @api buyProduct(sendTransaction) 购买物品
 * @param address 购买者
 * @param uint256 transId 一个交易的id
 * @event Transfer(address indexed _from, address indexed _to, uint256 _tokenId)
 * 
 * @api adventure(sendTransaction)
 * @param address player
 * @param uint256 foodId 食物的id
 * @param uint256[4] bagItems 背包物品，最多4个
 * @param uint16[4] rolls 分别是物品类型，季节，时间，地点，物品类型可以填0，因为在eth上已经随机roll了个点
 * @event AdventureSucc(uint256 itemId)
 * @event SynthesisSucc(uint256 itemId)
 * @event ItemDestroyed(address user, uint index, uint tokenId)
 * @event ItemCreated(address user, uint index, uint tokenId)
 * 
 * @api getAdventureInfo(call)
 * @param address player
 * @returns {[bool, bool, uint32, uint32, uint256[MAX_BAGLIST], uint256, uint256]}
 * 对应的是valid, finished, lastTime, colddownTime, bagList, rewardItem, synthesisItem
 * 
 */

contract('OsBaseData', function(accounts) {
  var instance;
  var gameConfig;
  let account = accounts[0];
  let other = accounts[1];
  it("instance", function(){
    return Oasis.deployed().then(function(ist){
      instance = ist;
      console.log("address =", instance.address);
      console.log("account =", account,"other =", other);
      instance.Transfer(function(err, logs){
        console.log("[Transfer] event =", logs);
      });

      instance.PlayerCreate(function(err, logs){
        console.log("[PlayerCreate] event =", logs);
      });

      instance.ItemCreated(function(err, logs){
        console.log("[ItemCreated] index =", logs.args.index.valueOf(),"itemId =", logs.args.tokenId.valueOf());
      });

      instance.ItemDestroyed(function(err, logs){
        console.log("[ItemDestroyed] index =", logs.args.index.valueOf(),"itemId =", logs.args.tokenId.valueOf());
      });

      instance.AdventureSucc(function(err, logs){
        console.log("[AdventureSucc] event itemId =", logs.args.itemId.valueOf());
        return;
      });

      instance.PropertiesAdd(function(err, logs){
        console.log("[PropertiesAdd] event =", logs.args.it.valueOf());
      });

      instance.SynthesisSucc(function(err, logs){
        console.log("[SynthesisSucc] event itemId =", logs.args.itemId.valueOf());
      });

    })
  })

  it("set valid", function () {
    return instance.setValid.sendTransaction("true").then(function(value){
       console.log("[set valid] value =", value);
       return GameConfig.deployed();
    }).then(function(config){
        gameConfig = config;
        console.log("[GameConfig::deployed] gameConfig =", gameConfig.address);
        return instance.setConfig.sendTransaction(gameConfig.address);
    }).then(function(trans){
        console.log("[Oasis::setConfig] trans =", trans);
        gameConfig.LOG(function(err, logs){
          console.log("[LOG] event itemId =", logs.args.itemId.valueOf(),"idnex =", logs.args.index.valueOf(), "i = ", logs.args.i.valueOf());
        });
    })
    
  });

  it("create player first", function () {
    instance.createPlayer.sendTransaction(account, "ww", [100, 100, 100, 100]).then(function(trans) {
      console.log("[Oasis::createPlayer first] trans =", trans);
    })
  });

  let token2 = 0;
  let food = 0;
  it("create player second", function () {
    return instance.createPlayer.sendTransaction(other, "qqq", [55, 55, 55, 55]).then(function(trans) {
      console.log("[Oasis::createPlayer second] trans =", trans);
      return instance.getPlayerInfo.call(other);
    }).then(function (info) {
        token2 = info[2][0];
        food = info[2][1];
        console.log("[Oasis::getPlayerInfo] token2 =", token2.valueOf(), "food =", food.valueOf());
        
        return instance.getItemInfo.call(info[2][0]);
    }).then(function(info){
      console.log("[Oasis::getItemInfo] token2 =", info[2].valueOf());
    })
  });
  
  let tokenId = 0;
  it("get item", function () {
    return instance.getPlayerInfo.call(account).then(function(info) {
      console.log("[Oasis::getPlayerInfo] info =", info[2][0].valueOf());
      return instance.getItemInfo.call( info[2][0]);
    }).then(function(info){
      tokenId = info[2];
      console.log("[Oasis::getItemInfo] info =", info);
    })
  });

  let transId = 0;
  it("create transaction", function() {
    return instance.getTransCount.call().then(function(count){
        console.log("[Oasis::getTransCount] count =", count.valueOf());
        return instance.createOneTrans.sendTransaction(account, tokenId, 1000);
    }).then(function(){
      return instance.getAllTransactions.call();
    }).then(function(list){
      console.log("[Oasis::getAllTransactions] list =", list);
      transId = list[0];
      return instance.getItemInfo.call(tokenId);
    }).then(function(info){
      console.log("[Oasis::getItemInfo] info =", info);
    }).catch(function(err){
      console.log("<<<>>>err =", err);
    })
  });
  
  it("buy product", function(){
      return instance.getTransactionInfo.call(transId).then(function(info){
          console.log("[Oasis::getTransactionInfo] info =", info);
          let a1 = web3.eth.getBalance(account).valueOf();
          let b1 = web3.eth.getBalance(other).valueOf();
          let c1 = web3.eth.getBalance(instance.address).valueOf();
          console.log(">>>>balance of a1 =", a1, "b1 =", b1,"c1 =", c1);
          return instance.buyProduct.sendTransaction(other, transId, {from : other, value : 1000});//value是价格
      }).then(function(info){
          console.log("[Oasis::buyProduct] info =", info);
          let a1 = web3.eth.getBalance(account).valueOf();
          let b1 = web3.eth.getBalance(other).valueOf();
          let c1 = web3.eth.getBalance(instance.address).valueOf();
          console.log(">>>>balance of a1 =", a1, "b1 =", b1, "c1 =", c1);
          return instance.getItemInfo.call(tokenId);
      }).then(function(info){
        console.log("[Oasis::getItemInfo] after buy info =", info);
        return instance.getPlayerInfo(other);
      }).then(function(info){
        console.log("[Oasis::getPlayerInfo] after buy other info =", info);
        return instance.getPlayerInfo(account);
      }).then(function(info){
        console.log("[Oasis::getPlayerInfo] after buy account info =", info);
      })
  });

  it("adventure", function(){
      return instance.getAdventureInfo.call(other).then(function(info){
          console.log("[Oasis::getAdventureInfo] info =", info, "tokenId =", tokenId);
          return instance.adventure.sendTransaction(other, food, [tokenId, token2], [1, 2, 3, 4]);
      }).then(function(){
          return instance.getAdventureInfo.call(other);
      }).then(function(info){
        console.log("[Oasis::getAdventureInfo] after adventure info =", info);
        return instance.getPlayerInfo.call(other);
      }).then(function(info){
          console.log("[instance::getPlayerInfo] after adventure info =", info);
      })
  })

  // it("transferItem", function () {
  //   let tokenId = 0;
  //   let b1 = 0;
  //   let b2 = 0;
  //   return instance.getPlayerInfo.call(other).then(function(info) {
  //     console.log("[Oasis::getPlayerInfo] info =", info[1]);
  //     return instance.getItemInfo.call( info[4][1]);
  //   }).then(function (item) {
  //     console.log("[Oasis::getItemInfo] item =", item[1].valueOf());
  //     tokenId = item[1];
  //     return instance.balanceOf.call(other);
  //   }).then(function (count) {
  //       b1 = web3.eth.getBalance(other).valueOf();
  //       a1 = web3.eth.getBalance(account).valueOf();
  //       c1 = web3.eth.getBalance(instance.address).valueOf();
  //       console.log("[Oasis::balanceOf] balance =", b1,"a1 =", a1, "c1 =", c1);
  //       return instance.transferFrom.sendTransaction(other, account, tokenId, {from : other, value:1902942500000000000});
  //   }).then(function (result) {
  //     console.log("[Oasis::transferFrom] result =", result);
  //     return instance.balanceOf.call( account);
  //   }).then(function (count) {
  //       b2 = web3.eth.getBalance(other).valueOf();
  //       a2 = web3.eth.getBalance(account).valueOf();
  //       c2 = web3.eth.getBalance(instance.address).valueOf();
  //       console.log("[Oasis::balanceOf] balance =", b2, "a2 =", a2, "c2 =", c2);
  //   })
  // });
});
